package com.atguigu.javase;

import java.util.HashSet;

/**
 * @Author: shenhao
 * @CreatTime: 2024/10/4 14:40
 * @Version: 1.0
 * 写一个hashcode冲突的案例
 */
public class HashConflictDemo {
    static class Book{
        int id;
    }
    public static void main(String[] args) {
        //m1();
        // 当我们正常的new对象的时候,new多少次对象,会有可能出现Hash冲突
        HashSet<Integer> hashSet = new HashSet<Integer>();
        for (int i = 1; i <= 11*10000; i++) {//10万次以后才会产生Hash冲突
            int hashCode = new Book().hashCode();
            if (hashSet.contains(hashCode)) {
                System.out.println("发生了Hash冲突,在第:"+i+"次,值为:"+hashCode);
            }else {
                hashSet.add(hashCode);
            }
        }
        System.out.println(hashSet.size());
    }

    private static void m1() {
        System.out.println("AA".hashCode());
        System.out.println("BB".hashCode());
        System.out.println();
        System.out.println("Aa".hashCode());
        System.out.println("BB".hashCode());
        System.out.println();
        System.out.println("柳柴".hashCode());
        System.out.println("柴柕".hashCode());
    }
}
